<template>
  <div class="login">
    <div class="avatar">
      <el-avatar
        size="large"
        src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png"
      ></el-avatar>
    </div>
    <el-form
      :model="userForm"
      status-icon
      :rules="formRules"
      ref="userForm"
      label-width="200px"
      class="demo-userForm"
    >
      <el-form-item label-width="50px" label="账户" prop="name">
        <el-input type="text" v-model="userForm.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label-width="50px" label="密码" prop="password">
        <el-input
          type="password"
          v-model="userForm.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('userForm')">提交</el-button>
        <el-button @click="resetForm('userForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    // 用户名验证规则之一：用户名不能为空
    const validateName = (rule, value, callback) => {
      value = value.trim();
      if (value == "") {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    // 密码验证规则
    const validatePassword = (rule, value, callback) => {
      const pattern = /^(\w){4,16}$/;
      value = value.trim();
      if (value == "") {
        callback(new Error("请输入密码"));
      }
      if (pattern.test(value)) {
        callback();
      } else {
        callback(new Error("只能输入4-16个字母、数字、下划线"));
      }
    };
    return {
      // 用户数据
      userForm: {
        name: "admin",
        password: "admin",
      },
      // 验证规则
      formRules: {
        name: [
          { requires: true, message: "请输入用户名", trigger: "blur" },
          { pattern: /^([\u4E00-\u9FA5])|admin$/, message: "只能输入中文" },
          { min: 2, max: 10, message: "长度在2-10之间" },
          { validator: validateName, trigger: "blur" },
        ],
        password: [{ validator: validatePassword, trigger: "blur" }],
      },
    };
  },
  methods: {
    // 登录
    submitForm() {
      const userInfo = {
        token: "test",
        username: this.userForm.name,
      };
      this.$refs.userForm.validate((valid) => {
        //验证成功
        if (valid) {
          //设置token
          if (this.userForm.name == "admin") {
            userInfo.token = this.userForm.name;
          }
          // 获取菜单
          this.$http({
            url: "/getMenus",
            method: "post",
            data: userInfo,
          }).then((res) => {
            const { data: result } = res;
            if (result.code == 0) {
              const { result: menuData } = result;
              // 存入本地session
              sessionStorage.setItem("menuData", JSON.stringify(menuData));
              sessionStorage.setItem("userInfo", JSON.stringify(userInfo));
              this.$message({
                type: "success",
                message: "登录成功！",
              });
              this.$router.push("/");
            } else if (result.code == 1) {
              this.$message({
                type: "error",
                message: "该用户不存在！",
              });
            }
          });
        } else {
          this.$message({
            type: "error",
            message: "验证失败！请输入符合规则的字符！",
          });
          return false;
        }
      });
    },
    // 重置表单
    resetForm() {
      this.$refs.userForm.resetFields();
    },
  },
};
</script>

<style scoped>
.login {
  width: 100%;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  margin-top: -30px;
}
.avatar {
  text-align: center;
  padding-bottom: 20px;
}
.el-avatar {
  width: 75px;
  height: 75px;
}
</style>
